package com.lenovo.manufacture.Adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.annotation.NonNull;

import java.util.List;

public abstract class IBaseAdapter<T> extends BaseAdapter {

    private final List<T> mList;

    public IBaseAdapter(@NonNull List<T> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public T getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("unchecked")
    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewProvider<T> viewProvider;

            if (convertView == null) {
                viewProvider = getViewProvider();
                convertView = viewProvider.createView(parent);
                convertView.setTag(viewProvider);
            } else {
                viewProvider = (ViewProvider<T>) convertView.getTag();
            }
            return viewProvider.init(getItem(position));
        }

    public List<T> getList() {
        return mList;
    }

    public abstract ViewProvider<T> getViewProvider();
}
